# send_command.py

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.auth.credentials import DerivedCredentials
from huaweicloudsdkcore.region.region import Region as coreRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkiotda.v5 import *

def send_motor_on_command():
    ak = "HPUAAXU6QJZDJVUGGR25"
    sk = "662qLiltWrSv6HPCJxjV9y20tl9OrY7nSiWsre8H"
    endpoint = "92aad16b61.st1.iotda-app.cn-east-3.myhuaweicloud.com"

    credentials = BasicCredentials(ak, sk).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())

    client = IoTDAClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(coreRegion(id="cn-east-3", endpoint=endpoint)) \
        .build()

    try:
        request = CreateCommandRequest()
        request.device_id = "6810ab2d3878983101479033_smart_pill_box_lm"
        request.body = DeviceCommandRequest(
            paras="{\"onoff\":\"ON\"}",
            command_name="motor_control"
        )
        response = client.create_command(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)